package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.domain.UtilVo;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private GoodsTypeDao goodsTypeDao;


    @Override
    @Transactional
    public Map<String, Object> getList(Integer page, Integer rows,String codeOrName) {
        List<Goods> list = goodsDao.getList(page, rows, codeOrName);
        Map<String,Object> map = new HashMap<>();
        int size = list.size();
        map.put("total",size);
        map.put("rows",list);
        return map;
    }

    @Override
    @Transactional
    public String save(Goods goods, String goodsId) {
        Integer result = null;
        goods.setInventoryQuantity(999);
        goods.setState(0);
        Integer goodsId1 = getGoodsById(goods.getGoodsId() - 1).getGoodsId();
        String goodsCode = goods.getGoodsCode();
        StringBuilder strBuilder = new StringBuilder(goodsCode);
        for (int i = 0; i < strBuilder.length(); i++) {

        }
        if ("".equals(goodsId)||goodsId==null){
            System.out.println("goods = " + goods);
            result = goodsDao.insert(goods);
        }else {
            result = goodsDao.update(goods,goodsId);
        }
        return UtilVo.toGson(UtilVo.toService(result));
    }

    public Goods getGoodsById(Integer goodsId){
        return goodsDao.getGoodsById(goodsId);
    }

    public GoodsType getGoodsType(Integer goodsTypeId){
        return goodsTypeDao.selectGoodsType(goodsTypeId);
    }
}
